// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-05 21:45
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.TREE;


import com.DataStruct.Tree.TwoBinaryTree.TreeNode;

public class InvertTree {
    /**
     * 翻转二叉树 : 镜像二叉树
     */

    TreeNode node = new TreeNode(0);

    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        node.val = root.val;
        travel(root, node);
        return node;
    }

    private void travel(TreeNode root, TreeNode node) {
        if (root.right != null) {
            node.left = new TreeNode(root.right.val);
            travel(root.right, node.left);
        }
        if (root.left != null) {
            node.right = new TreeNode(root.left.val);
            travel(root.left, node.right);
        }
    }
}

